﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_Friends_News : IDisposable
    {
        private SqlDataAdapter dscommand;

        public b_Friends_News()
        {
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;
            if (dscommand != null)
            {
                if (dscommand.SelectCommand != null)
                {
                    if (dscommand.SelectCommand.Connection != null)
                        dscommand.SelectCommand.Connection.Dispose();
                    dscommand.SelectCommand.Dispose();
                }
                dscommand.Dispose();
                dscommand = null;
            }
        }
        //
        //指令资源分配方式的代码段，与逻辑无关
        //
        public b_Friends_Newsdata getnewsbynewsid(int newsid)
        {
            //
            //用新鲜事编号返回一个新鲜事
            //
            b_Friends_Newsdata newdata = new b_Friends_Newsdata();
            string selectstring = "SELECT * FROM b_Friends_News WHERE News_ID = " + newsid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends_News");
            tempconn.Close();
            return (newdata);
        }

        public b_Friends_Newsdata getnewsbyboth(string username, int diaryid)
        {
            //
            //
            //
            b_Friends_Newsdata newdata = new b_Friends_Newsdata();
            string selectstring = "SELECT * FROM b_Friends_News WHERE Host_User_ID ='" + username + "' AND Diary_ID ="+diaryid .ToString ();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends_News");
            tempconn.Close();
            return (newdata);
        }



        public b_Friends_Newsdata getnewsbyusername(string username)
        {
            //
            //输入Host_User_ID(接收新鲜事的人），得到给他的全部新鲜事（已读和未读的都有）
            //
            b_Friends_Newsdata newdata = new b_Friends_Newsdata();
            string selectstring = "SELECT * FROM b_Friends_News WHERE Host_User_ID ='" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends_News");
            tempconn.Close();
            return (newdata);

        }

        public b_Friends_Newsdata GetNewsNotReadByName(string username)
        {
            //
            //输入Host_User_ID,得到给他的全部未读的新鲜事
            //
            b_Friends_Newsdata newdata = new b_Friends_Newsdata();
            string selectstring = "SELECT * FROM b_Friends_News WHERE Host_User_ID ='" + username + "' AND News_Cover = 'False' ";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends_News");
            tempconn.Close();
            return (newdata);
        }

        public int CountNewsNotReadByName(string username)
        {
            //
            //获得一个用户未读的新鲜事数目
            //
            return (GetNewsNotReadByName(username).Tables["b_Friends_News"].Rows.Count);
        }

        public bool InsertNewsofDiary(string hostid, b_Diarydata diary)
        {
            //输入一个接收用户邮箱和一个日志，插入到新鲜事中去
            //标记未读
            string diarytitle = diary.Tables["b_Diary"].Rows[0]["Diary_Title"].ToString();
            string guestid = diary.Tables["b_Diary"].Rows[0]["User_ID"].ToString();
            string diarydate = diary.Tables["b_Diary"].Rows[0]["Diary_Date"].ToString();
            string diaryid = diary.Tables["b_Diary"].Rows[0]["Diary_ID"].ToString();
            string insertstring = "INSERT b_Friends_News (News_Kind,Host_User_ID,Guest_User_ID,News_Title,News_Date,News_Cover,Diary_ID) VALUES (1,'" + hostid + "','" + guestid + "','" + diarytitle + "','" + diarydate + "','False',"+diaryid+")";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(insertstring, cn);
            cn.Open();
            int records = cmd.ExecuteNonQuery();
            cn.Close();
            if (records == 0)
                return false;
            else
                return true;
        }

        public bool InsertNewsofActivity(string hostid, b_Activitydata act)
        {
            //输入一个接受用户邮箱和一个活动，插入到新鲜事中去
            //标记未读
            string acttitle = act.Tables["b_Activity"].Rows[0]["Act_Name"].ToString();
            string guestid = act.Tables["b_Activity"].Rows[0]["Act_Founder"].ToString();

            string actdate = act.Tables["b_Activity"].Rows[0]["Act_Pub_Date"].ToString();
            string actid = act.Tables["b_Activity"].Rows[0]["Act_ID"].ToString();
            string insertstring = "INSERT b_Friends_News (News_Kind,Host_User_ID,Guest_User_ID,News_Title,News_Date,News_Cover,Diary_ID) VALUES(2,'" + hostid + "','" + guestid + "','" + acttitle + "','" + actdate + "','False',"+actid+")";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(insertstring, cn);
            cn.Open();
            int records = cmd.ExecuteNonQuery();
            cn.Close();
            if (records == 0)
                return false;
            else
                return true;
        }

        public bool ConfirmNews(b_Friends_Newsdata tempdata)
        {
            //输入一个新鲜事，确认之。
            //确认成功返回true,否则返回false
            int newsid = Int32.Parse(tempdata.Tables["b_Friends_News"].Rows[0]["News_ID"].ToString());
            b_Friends_Newsdata newdata = new b_Friends_Newsdata();
            string selectstring = "SELECT * FROM b_Friends_Newsdata WHERE News_ID =" + newsid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Friends_News");
            if (newdata.Tables["b_Friends_News"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_Friends_News"].Rows[0]["News_Cover"] = true;
                dscommand.Update(newdata, "b_Diary");
                tempconn.Close();
                return (true);
            }
        }

        public bool DeleteNews(b_Friends_Newsdata tempdata)
        {
            //输入一个新鲜事，删除之
            //删除成功返回true,否则返回false
            int newsid = Int32.Parse(tempdata.Tables["b_Friends_News"].Rows[0]["News_ID"].ToString());
            string deletestring = "DELETE b_Friends_News WHERE News_ID = " + newsid.ToString();
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(deletestring, cn);
            cn.Open();
            int Records = cmd.ExecuteNonQuery();
            cn.Close();
            if (Records == 0)
                return false;
            else
                return true;
        }
        public bool DeleteNewsbyhostname(string username)
        {
            //
            //删除一个用户接受的所有新鲜事，成功返回true,失败返回false
            //
            string deletestring = "DELETE b_Friends_News WHERE Host_User_ID ='" + username + "'";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(deletestring, cn);
            cn.Open();
            int Records = cmd.ExecuteNonQuery();
            cn.Close();
            if (Records == 0)
                return false;
            else
                return true;
        }
        public bool DeleteNewsbyguestname(string username)
        {
            //
            //删除一个用户发出的所有新鲜事，成功返回true,失败返回false
            //
            string deletestring = "DELETE b_Friends_News WHERE Guest_User_ID ='" + username + "'";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(deletestring, cn);
            cn.Open();
            int Records = cmd.ExecuteNonQuery();
            cn.Close();
            if (Records == 0)
                return false;
            else
                return true;
        }


    }
}
